package com.embhealth.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.embhealth.constant.MessageConstant;
import com.embhealth.constant.RedisConstant;
import com.embhealth.entity.LogException.DeleteAddressException;
import com.embhealth.entity.PageResult;
import com.embhealth.entity.QueryPageBean;
import com.embhealth.entity.Result;
import com.embhealth.pojo.Address;
import com.embhealth.pojo.CheckItem;
import com.embhealth.pojo.Setmeal;
import com.embhealth.service.AddressService;
import com.embhealth.service.CheckItemService;
import com.embhealth.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.io.IOException;
import java.util.UUID;

/**
 * 体检机构管理
 */
@RestController
@RequestMapping("/address")
public class AddressController {

    @Reference
    private AddressService addressService;
    @Autowired
    private JedisPool jedisPool;

    /**
     * 图像文件上传
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam("imgFile") MultipartFile imgFile){
        //获取文件上传的原始文件名
        String originalFilename = imgFile.getOriginalFilename(); //gthhnsafse.jpg
        //获取文件名的扩展名
        int index = originalFilename.lastIndexOf(".");
        String extention = originalFilename.substring(index); // .jpg
        //使用UUID生成文件名
        String fileName = UUID.randomUUID().toString().replaceAll("-", "") + extention;
        try {
            Thread.sleep(300);
            //上传文件到七牛云
            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            //将上传的图片名称存到redis,基于redis的set集合存储
            jedisPool.getResource().sadd(RedisConstant.Address_PIC_RESOURCES, fileName);
            //表示文件上传成功,携带上传的文件名数据响应给浏览器
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            //表示文件上传失败
            return new Result(true, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }
    
    //体检机构地址信息分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = addressService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 新增体检机构地址信息
     * @param address
     * @return
     */
    @RequestMapping("/add")
    public Result add(@RequestBody Address address){
        try{
            addressService.add(address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_ADDRESS_FAIL);
        }
        return new Result(true,MessageConstant.ADD_ADDRESS_SUCCESS);
    }

    /**
     * 编辑体检机构地址信息回显
     * @param id
     * @return
     */
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Address address=addressService.findById(id);
            return new Result(true,MessageConstant.EDIT_ADDRESS_SUCCESS,address);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_ADDRESS_FAIL);
        }
    }

    /**
     * 删除体检机构地址信息
     * @param id
     * @param img
     * @return
     */
    @RequestMapping("/deleteById")
    public Result delete(Integer id,String img){
        try {
            addressService.delete(id);
            QiniuUtils.deleteFileFromQiniu(img);
            if (img!=null && jedisPool.getResource().sismember(RedisConstant.Address_PIC_DB_RESOURCES, img)){
                jedisPool.getResource().hdel(RedisConstant.Address_PIC_DB_RESOURCES,img);
            }
            return new Result(true,MessageConstant.DELETE_ADDRESS_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            if (e instanceof DeleteAddressException) {
                return  new Result(false,"当前体检机构已关联到预约订单,请优先删除地址与预约的中间表的相关信息");
            }
            return new Result(false,MessageConstant.DELETE_ADDRESS_FAIL);
        }
    }

    //编辑体检机构地址信息
    @RequestMapping("/edit")
    public Result edit(@RequestBody Address address) {
        try {
            addressService.edit(address);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ADDRESS_FAIL);
        }
        return new Result(true, MessageConstant.EDIT_ADDRESS_SUCCESS);
    }
}
